<?php

/**
 * @author martin
 * @copyright 2009
 */

if ($GLOBALS['setting']['allow_reg'] == 1) {

    function __doRegister()
    {
        $user_test = Registry::get('db')->query("SELECT `username` FROM `{prefix}users` WHERE LOWER(`username`) = LOWER('" .
            Registry::get('db')->real_escape_string($_POST['username']) . "')");

        $email_test = Registry::get('db')->query("SELECT `email` FROM `{prefix}users` WHERE `email` = '" .
            Registry::get('db')->real_escape_string($_POST['email']) . "'");

        if ($user_test->num_rows == 0 && $email_test->num_rows == 0) {
            $hash = new Hash(Registry::get('db')->real_escape_string($_POST['username']),
                Registry::get('db')->real_escape_string($_POST['password']));
            $hash->hash();
            if (empty($_POST['display_name'])) {
                $display_name = Registry::get('db')->real_escape_string($_POST['username']);
            } else {
                $display_name = Registry::get('db')->real_escape_string($_POST['display_name']);
            }
            $query = Registry::get('db')->query("INSERT INTO {prefix}users (username, display_name, password, salt, email) VALUES('" .
                Registry::get('db')->real_escape_string(strtolower($_POST['username'])) . "', '" .
                $display_name . "', '" . $hash->password . "', '" . $hash->salt . "','" .
                Registry::get('db')->real_escape_string($_POST['email']) . "')");
            echo Lang::getFront('com_user', 'register_success');
            echo '<p>' . Lang::getFront('com_user', 'register_yourname') . ' : <strong>' .
                Registry::get('db')->real_escape_string(strtolower($_POST['username'])) .
                '</strong></p>';
        }
    }

    $form = new Form;

    $form->addQuestion(Lang::getFront('com_user', 'register_captcha_q'), Lang::
        getFront('com_user', 'register_captcha_a'));

    $form->addGroup(Lang::getFront('com_user', 'register_main'));

    $form->addTextbox('username', Lang::getFront('com_user', 'username'), $_POST['username'])->
        addRegex('/^[a-z0-9]*$/i', Lang::getFront('com_user', 'register_wrong_user'))->
        notEmpty(Lang::getFront('com_user', 'register_wrong_user'));

    $form->addPassword('password', Lang::getFront('com_user', 'password'), $_POST['password'])->
        setMinLength(5, Lang::getFront('com_user', 'register_wrong_pass'))->notEmpty(Lang::
        getFront('com_user', 'register_wrong_pass'));

    $form->addTextbox('email', Lang::getFront('com_user', 'email'), $_POST['email'])->
        addRegex(Form_vars::Email, Lang::getFront('com_user', 'register_wrong_email'))->
        notEmpty(Lang::getFront('com_user', 'register_wrong_email'));

    $form->addGroup(Lang::getFront('com_user', 'register_other'));

    $form->addTextbox('display_name', Lang::getFront('com_user', 'display_name'), $_POST['display_name']);

    $form->addCaptcha();

    $form->addSubmit('submit', Lang::getFrontGlobal('submit'));

    echo $form;


    if ($form->isSubmitted()) {
        if ($form->isHuman()) {

            if ($form->isValid()) {

                __doRegister();

            } else {
                echo $form->getMessage();
            }

        } else {
            echo Lang::getFront('com_user', 'register_wrong_code');
        }
    }

} else {
    echo Lang::getFront('com_user', 'register_disabled');
}
?>